package com.itheima.Utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MybatisUtils {
    private static   SqlSessionFactory sqlSessionFactory;
    static {
        try {
            InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");//这里传入核心配置文件路径
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //定义一个获取sqlsessionfactory对象静态方法
    public static SqlSessionFactory getSqlSessionFactory(){
        return  sqlSessionFactory;
    }
    //定义一个获取会话对象的静态方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
    //定义释放资源静态方法
    public static void close(SqlSession s){
        if (s!=null){
            s.close();
        }
    }
}
